package com.example.sharesinfo.fragment.stock;


import android.content.res.Resources;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.sharesinfo.R;
import com.example.sharesinfo.bean.ThreePlatesTopBean;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MarketBanKuaiFragment extends Fragment {
    /**
     * 没做:
     * 问题：快速涨幅没有api
     */


    String LogTag = "MarketBanKuaiFragment:";

    //行业板块
    private List<TextView> listHyName = new ArrayList<>();
    private List<TextView> listHyGain = new ArrayList<>();
    private List<TextView> listHyIndustry = new ArrayList<>();

    //概念模块
    private List<TextView> listGlName = new ArrayList<>();
    private List<TextView> listGlGain = new ArrayList<>();
    private List<TextView> listGlIndustry = new ArrayList<>();

    //地域模块
    private List<TextView> listDyName = new ArrayList<>();
    private List<TextView> listDyGain = new ArrayList<>();
    private List<TextView> listDyIndustry = new ArrayList<>();
    Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 200:
                    JSONArray list = (JSONArray) msg.obj;
                    try {
                        JSONArray industry = ((JSONObject) list.get(0)).getJSONArray("行业板块");
                        JSONArray concept = ((JSONObject) list.get(1)).getJSONArray("概念板块");
                        JSONArray area = ((JSONObject) list.get(2)).getJSONArray("地域板块");

                        for (int i = 0; i < industry.length(); i++) {
                            listHyName.get(i).setText(((JSONObject) industry.get(i)).getString("名称"));
                            listHyGain.get(i).setText(((JSONObject) industry.get(i)).getString("涨幅"));
                            listHyIndustry.get(i).setText(((JSONObject) industry.get(i)).getString("领涨股"));
                        }
                        for (int i = 0; i < concept.length(); i++) {
                            listGlName.get(i).setText(((JSONObject) concept.get(i)).getString("名称"));
                            listGlGain.get(i).setText(((JSONObject) concept.get(i)).getString("涨幅"));
                            listGlIndustry.get(i).setText(((JSONObject) concept.get(i)).getString("领涨股"));
                        }
                        for (int i = 0; i < area.length(); i++) {
                            listDyName.get(i).setText(((JSONObject) area.get(i)).getString("名称"));
                            listDyGain.get(i).setText(((JSONObject) area.get(i)).getString("涨幅"));
                            listDyIndustry.get(i).setText(((JSONObject) area.get(i)).getString("领涨股"));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 404:

                    break;
            }
            return false;
        }
    });

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_market_ban_kuai, null);
        initView(view);
        queryDatas();
        return view;
    }

    private void queryDatas() {
        String url = "http://119.23.221.142/api/stock/GetThreePlatesTop6";
        OkHttpUtils.getInstance().Get(url, new OkHttpUtils.okHttpListener() {
            final Message message = mHandler.obtainMessage();

            @Override
            public void OnSuccess(String s, int code) throws JSONException {
                JSONObject response = new JSONObject(s);
                JSONArray list = response.getJSONArray("data");
                message.what = code;
                message.obj = list;
                mHandler.sendMessage(message);
            }

            @Override
            public void OnFailed(String s) {
                message.what = 404;
                message.obj = s;
                mHandler.sendMessage(message);
            }
        });

    }

    private void initView(View view) {
        Resources res = getResources();
        for (int i = 1; i <= 6; i++) {
            //行业板块
            int tv_hy_name_id = res.getIdentifier("tv_hy_name_" + i, "id", Constants.PACKAGENAME);
            int tv_hy_gain_id = res.getIdentifier("tv_hy_gain_" + i, "id", Constants.PACKAGENAME);
            int tv_hy_industry_id = res.getIdentifier("tv_hy_industry_" + i, "id", Constants.PACKAGENAME);
            TextView tv_hy_name = view.findViewById(tv_hy_name_id);
            TextView tv_hy_gain = view.findViewById(tv_hy_gain_id);
            TextView tv_hy_industry = view.findViewById(tv_hy_industry_id);
            listHyName.add(tv_hy_name);
            listHyGain.add(tv_hy_gain);
            listHyIndustry.add(tv_hy_industry);
            //概念模块
            int tv_gl_name_id = res.getIdentifier("tv_gl_name_" + i, "id", Constants.PACKAGENAME);
            int tv_gl_gain_id = res.getIdentifier("tv_gl_gain_" + i, "id", Constants.PACKAGENAME);
            int tv_gl_industry_id = res.getIdentifier("tv_gl_industry_" + i, "id", Constants.PACKAGENAME);
            TextView tv_gl_name = view.findViewById(tv_gl_name_id);
            TextView tv_gl_gain = view.findViewById(tv_gl_gain_id);
            TextView tv_gl_industry = view.findViewById(tv_gl_industry_id);
            listGlName.add(tv_gl_name);
            listGlGain.add(tv_gl_gain);
            listGlIndustry.add(tv_gl_industry);
            //地域模块
            int tv_dy_name_id = res.getIdentifier("tv_dy_name_" + i, "id", Constants.PACKAGENAME);
            int tv_dy_gain_id = res.getIdentifier("tv_dy_gain_" + i, "id", Constants.PACKAGENAME);
            int tv_dy_industry_id = res.getIdentifier("tv_dy_industry_" + i, "id", Constants.PACKAGENAME);
            TextView tv_dy_name = view.findViewById(tv_dy_name_id);
            TextView tv_dy_gain = view.findViewById(tv_dy_gain_id);
            TextView tv_dy_industry = view.findViewById(tv_dy_industry_id);
            listDyName.add(tv_dy_name);
            listDyGain.add(tv_dy_gain);
            listDyIndustry.add(tv_dy_industry);
        }

    }

}
